package com.chenwut.threadDemo.threadsafe1;

/**
 * @author ChenPi
 * @since 2024/09/18 13:50
 */
public class MyThread extends Thread {
    static int ticket = 0;
    static Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (obj) {
                if (ticket < 100) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ticket++;
                    System.out.println(getName() + "正在卖第" + ticket + "张票");
                } else {
                    break;
                }
            }

        }

    }
}
